package Net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import JsonModel.FamiliarFriend;
import Utilities.Utility;
import android.content.Context;
import android.util.Log;

public class JsonParse {
	public static final String url = "http://www.ungdungviet.zz.mu/smartnote/note.php";
	
	public JsonParse (){}
	
	//get all group
	public String getUserInGroup(
			Context c, String groupId, String userId, int page) {		
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getuseringroup"));
		value.add(new BasicNameValuePair("userid", userId));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		value.add(new BasicNameValuePair("page", page+""));
		value.add(new BasicNameValuePair("groupid", groupId));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	//get all group
	public String getAllGroup(
			Context c, String userId, int page) {		
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getallgroup"));
		value.add(new BasicNameValuePair("userid", userId));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		value.add(new BasicNameValuePair("page", page+""));
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	
	//send request attend to friend list
	public String sendRequestAcceptFriendList(
			Context c, String requestId, String isAccept) {		
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "isacceptmakefriend"));
		value.add(new BasicNameValuePair("requestid", requestId));
		value.add(new BasicNameValuePair("isaccept", isAccept));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	//send request attend to group
	public String sendRequestAttendGroup(
			Context c, String userId, String groupId, ArrayList<FamiliarFriend> f) {		
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "requestattendtogroup"));
		value.add(new BasicNameValuePair("groupid", groupId));
		value.add(new BasicNameValuePair("usersend", userId));
		for (int i=0;i<f.size();i++) {
			value.add(new BasicNameValuePair("userreceive["+i+"]", f.get(i).getUserId()));
		}
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}

	//get friend list
	public String getFriendList(
			Context c, String userId, int page) {		
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getfriendlist"));
		value.add(new BasicNameValuePair("userid", userId));
		value.add(new BasicNameValuePair("page", page+""));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}

	//create group
	public String createGroup(Context c, String userId,
			String expried, String categoryId, String logoId, String title, int isPublish) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "creategroup"));
		value.add(new BasicNameValuePair("monitor", userId));
		value.add(new BasicNameValuePair("expried", expried));	
		value.add(new BasicNameValuePair("groupcatid", categoryId));
		value.add(new BasicNameValuePair("logoid", logoId));	
		value.add(new BasicNameValuePair("title", title));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		value.add(new BasicNameValuePair("ispublish", isPublish+""));	
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	
	//get list friend
	public String isAcceptMakeFriend(Context c, String[] params) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "isacceptmakefriend"));
		value.add(new BasicNameValuePair("requestid", params[0]));
		value.add(new BasicNameValuePair("isaccept", params[1]));	
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	
	//get list friend
	public String getListAcceptMakeFriend(Context c, String[] params) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getlistrequestmakefriend"));
		value.add(new BasicNameValuePair("userreceive", params[0]));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));	
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	
	//send request make friend
	public String sendRequestMakeFriend(Context c, String[] params, String string)//sender, receiver
	{
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "requestmakefriend"));
		value.add(new BasicNameValuePair("userroot", params[0]));
		value.add(new BasicNameValuePair("userreceive", string));		
		Log.d("log","userreceive:"+string);
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	
	//cancel request make friend
	public String sendCancelMakeFriend(Context c, String[] params)//sender, receiver
	{
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "cancelmakefriend"));
		value.add(new BasicNameValuePair("userroot", params[0]));
		value.add(new BasicNameValuePair("userreceive", params[1]));		
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	
	//get search by key
	public String getSearchFriend(Context c, String key,String userid, int page)
	{
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getuserfromgmail"));
		value.add(new BasicNameValuePair("gmail", key));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		value.add(new BasicNameValuePair("page", page+""));
		value.add(new BasicNameValuePair("userid", userid));
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	
	//get more resemble gal
	public String getResembleFriend(Context c, String userid, int page)
	{
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "searchfamiliarfriend"));
		value.add(new BasicNameValuePair("userid", userid));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		value.add(new BasicNameValuePair("page", page+""));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	
	//get logo group from server
	public String getGroupLogo(Context c) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getlogo"));
		value.add(new BasicNameValuePair("groupingid", "2"));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		value.add(new BasicNameValuePair("kind", "1"));
		value.add(new BasicNameValuePair("gender", "-1"));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	//get logo from server
	public String getUserLogo(Context c) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getlogo"));
		value.add(new BasicNameValuePair("groupingid", "1"));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		value.add(new BasicNameValuePair("kind", "1"));
		value.add(new BasicNameValuePair("gender", "2"));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	//register account
	public String registerAccount(String[] params, Context c)
	{//gmail,nickname,pass,isGender+"",sBirthday.toString(),platform+"",type,gcmId
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "register"));
		value.add(new BasicNameValuePair("gmail", params[0]));
		value.add(new BasicNameValuePair("password", params[2]));
		value.add(new BasicNameValuePair("birthday", params[4]));
		value.add(new BasicNameValuePair("platform", params[5]));
		value.add(new BasicNameValuePair("deviceid", params[7]));
		value.add(new BasicNameValuePair("logo", params[8]));
		value.add(new BasicNameValuePair("nickname", params[1]));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		value.add(new BasicNameValuePair("type", params[6]));
		value.add(new BasicNameValuePair("gender", params[3]));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	//login account
	public String loginAccount(String[] params, Context c)
	{//gmail,pass,gcmId,type
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "login"));
		value.add(new BasicNameValuePair("gmail", params[0]));
		value.add(new BasicNameValuePair("dpi", Utility.getDPI(c)));
		value.add(new BasicNameValuePair("password", params[1]));
		value.add(new BasicNameValuePair("type", "0"));
		value.add(new BasicNameValuePair("platform", params[3]));
		value.add(new BasicNameValuePair("deviceid", params[2]));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json; 
	}
	
	//send email forget password
	public String SendEmailForgetPassword(String email)
	{
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "forgetpassword"));
		value.add(new BasicNameValuePair("gmail", email));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json; 
	}
	
	//load categories
	public String loadCategories() {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getcatgroup"));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json; 
	}
	//get json from url and namvaluepair (parmams: url, namevaluepair)
	public String getJsonFromURLPostNameValuePair(String url, List<NameValuePair> doiso) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		String noidung = "";
		try {
			UrlEncodedFormEntity e = new UrlEncodedFormEntity(doiso,
					"UTF-8");
			post.setEntity(e);
			ResponseHandler<String> handler = new BasicResponseHandler();
			noidung = client.execute(post, handler);
		
		} catch (Exception e) {
			Log.d("error",e.getMessage());
			e.printStackTrace();
		}
		return noidung;		
	}

	

	

}
